package com.corpdata.app.message.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.corpdata.app.contract.entity.ContractInfo;
import com.corpdata.app.contract.entity.ContractReceivable;
import com.corpdata.app.message.dao.SysMessageMapper;
import com.corpdata.app.message.dao.SysMessageUserMapper;
import com.corpdata.app.message.entity.SysMessage;
import com.corpdata.app.message.entity.SysMessageUser;
import com.corpdata.app.message.service.SysMessageService;
import com.corpdata.common.result.Result;
import com.corpdata.common.result.util.ResultUtil;
import com.corpdata.common.utils.CorpdataUtil;
import com.corpdata.core.base.AbstractBaseService;

/**
 * 
 * 
 * @author wu
 * @email 64088839@qq.com
 * @date 2018-08-02 10:54:11
 */
@Service
@Transactional
public class SysMessageServiceImpl extends AbstractBaseService<SysMessage> implements SysMessageService {
	
	@Autowired
	private SysMessageMapper msgMapper;
	
	@Autowired
	private SysMessageUserMapper muMapper;
	
	/**新增消息列表录入信息
	 * @param obj-实体类
	 * @param type-消息类型
	 * @param msgList
	 * @param muList
	 * @param flag消息类型下的分类
	 */
	public void initList(Object obj,int type,List<SysMessage> msgList,List<SysMessageUser> muList,int flag){
		Date now = new Date();
		SysMessage msg = new SysMessage();
		SysMessageUser mu = new SysMessageUser();
		msg.setId(CorpdataUtil.getUUID());
		msg.setCreater(null);
		msg.setCreated(now);
		mu.setCreater(null);
		mu.setCreated(now);
		initObj(msg, mu, obj, type,flag);
		msgList.add(msg);
		muList.add(mu);
	}
	//实体类录入信息
	public void initObj(SysMessage msg,SysMessageUser mu,Object obj,int type,int flag){
		if(type == 1){//发送账单通知
			ContractReceivable re = (ContractReceivable) obj;
			if(flag == 0){
				msg.setSubject("账单通知");
				msg.setMessageContent(re.getTeam().getFullName()+" 团队下编号为："+re.getContract().getContractNumber()+"的合同账单待缴租");
			}else{
				msg.setSubject("催款账单通知");
				msg.setMessageContent(re.getTeam().getFullName()+" 团队下编号为："+re.getContract().getContractNumber()+"的合同账单已逾期");
			}
			msg.setMessageUri("/app/contractInfo/detailCenter/"+re.getContract().getId());
			msg.setDocid(re.getId());
			msg.setFkProject(re.getContract().getFkProject());
			mu.setUserid(re.getCreater());
		}else if(type == 2){//合同到期提醒
			ContractInfo ci  = (ContractInfo) obj;
			msg.setSubject("合同到期提醒");
			msg.setMessageContent(ci.getTeam().getFullName()+" 团队下编号为："+ci.getContractNumber()+"的合同将于30天后到期");
			msg.setMessageUri("/app/contractInfo/detailCenter/"+ci.getId());
			msg.setDocid(ci.getId());
			msg.setFkProject(ci.getFkProject());
			mu.setUserid(ci.getCreater());
		}
		msg.setMessageType(type);
		mu.setId(CorpdataUtil.getUUID());
		mu.setFkMessage(msg.getId());
		mu.setReadState(false);
		mu.setDeleteState(false);
		
	}
	//消息列表新增
	@Override
	public Result insertList(List<SysMessage> msgList, List<SysMessageUser> muList) {
		int i = msgMapper.insertList(msgList);
		if(i > 0){
			i = muMapper.insertList(muList);
		}
		if(i > 0){
			return ResultUtil.success();
		}else{
			return ResultUtil.fail();
		}
	}
}
